package com.cyclingAssistant.overlays;

import java.util.ArrayList;
import java.util.List;

import org.osmdroid.DefaultResourceProxyImpl;
import org.osmdroid.ResourceProxy;
import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.MapView;
import org.osmdroid.views.overlay.ItemizedIconOverlay;
import org.osmdroid.views.overlay.OverlayItem;
import org.osmdroid.views.overlay.PathOverlay;

import com.cyclingAssistant.project.CyclingAssistantActivity;
import com.cyclingAssistant.project.R;
import com.cyclingAssistant.project.RoutePlan;
import com.cyclingAssistant.project.TabMenu;
import com.cyclingAssistant.route.DrawRoute;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.graphics.drawable.Drawable;
import android.media.SoundPool.OnLoadCompleteListener;
import android.util.Log;
import android.widget.EditText;
import android.widget.TabHost;

public class CAItemizedOverlay extends ItemizedIconOverlay<OverlayItem> {
	static Context mContext;

	static Boolean isInRouteOptions = false;
	DrawRoute pathOverlay;
	static MapView mapView;
	static UpdateMap updateMap;

	public CAItemizedOverlay(Drawable pDefaultMarker, Context context,
			Boolean option, DrawRoute overlay, MapView view) {
		super(new ArrayList<OverlayItem>(), pDefaultMarker,
				new ItemTapListener(), new DefaultResourceProxyImpl(context));
		mContext = context;
		isInRouteOptions = option;
		pathOverlay = overlay;
		mapView = view;
		updateMap = new UpdateMap();
	}

	public CAItemizedOverlay(
			List<OverlayItem> pList,
			Drawable pDefaultMarker,
			org.osmdroid.views.overlay.ItemizedIconOverlay.OnItemGestureListener<OverlayItem> pOnItemGestureListener,
			ResourceProxy pResourceProxy) {
		super(pList, pDefaultMarker, pOnItemGestureListener, pResourceProxy);
		// TODO Auto-generated constructor stub
	}

	public class UpdateMap implements OnClickListener {

		@Override
		public void onClick(DialogInterface dialog, int which) {
			// TODO Auto-generated method stub
			mapView.getOverlays().clear();
			GeoPoint locPoint1 = pathOverlay.getEndPoint();

			Drawable draw = mContext.getResources()
					.getDrawable(R.drawable.info);
			OverlayItem overlayitem1 = new OverlayItem("Route Options", "What option do you want on this route?",
					locPoint1);
			ItemizedIconOverlay<OverlayItem> end_overlay = new EndPointItemOverlay(
					draw, mContext);

			end_overlay.addItem(overlayitem1);
			mapView.getOverlays().add(pathOverlay);
			mapView.getOverlays().add(end_overlay);
		}
	}

	public static class ItemTapListener implements
			OnItemGestureListener<OverlayItem> {

		@Override
		public boolean onItemLongPress(int index, OverlayItem item) {
			// TODO Auto-generated method stub
			return false;
		}

		@Override
		public boolean onItemSingleTapUp(int index, final OverlayItem item) {
			// TODO Auto-generated method stub

			if (!isInRouteOptions) {
				AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
				dialog.setTitle(item.getTitle());
				dialog.setMessage(item.getSnippet());
				dialog.setPositiveButton("from here", new OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {

						TabHost tabHost = (TabHost) TabMenu.tabHost;
						RoutePlan.from.setText(item.getTitle());
						tabHost.setCurrentTab(1);
					}
				});
				dialog.setNeutralButton("to here", new OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						TabHost tabHost = (TabHost) TabMenu.tabHost;
						RoutePlan.to.setText(item.getTitle());
						tabHost.setCurrentTab(1);

					}
				});
				dialog.setNegativeButton("back", new OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub

					}
				});
				dialog.show();
			} else {
				AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
				dialog.setTitle(item.getTitle());
				dialog.setMessage(item.getSnippet());
				dialog.setPositiveButton("Follow this route", updateMap);
				dialog.setNegativeButton("Cancel", new OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
					}
				});
				dialog.show();
			}

			return true;
		}
	}
}